<?php
$name = system_controller_front::getInstance()->getRequest()->getActionName();
$this->render('include/sortLink.phtml');
$this->headScript()->appendFile(_STATIC_HTTP . '/jquery/checkAll.js');
echo $this->render('picture/special/_tab.phtml');
if(!function_exists('__sortLink')) {
	function __sortLink() {}
}
?>
<?php $this->headScript()->captureStart();?>
$(document).ready(function(){
	$('a.edit-modify').click(function(){
		var id = $(this).attr('rel');
		var input = $('input#keyword-' + id);
		$('#tag-' + id).hide();
		$('#tag-' + id + '-input').show();
		input.focus();
		return false;
	});
	$('a.edit-cancel').click(function(){
		var id = $(this).attr('rel');
		var input = $('input#keyword-' + id);
		$('#tag-' + id).show();
		$('#tag-' + id + '-input').hide();
		input.val(input.attr('rel'));
		return false;
	});
});
<?php $this->headScript()->captureEnd();?>
<form action="" method="post">
<div class="body">
	<table class="list">
		<tr>
			<th class="id"></th>
			<th class="narrow sort" title="<?php echo $this->language('Display Order');?>"><?php echo __sortLink('display_order', 'D');?></th>
			<th><?php echo $this->language('INFORMATION');?></th>
			<th class="narrow sort" title="<?php echo $this->language('Photo Total Count');?>"><?php echo __sortLink('photo', 'PHOTO');?></th>
			<th class="time sort"><?php echo __sortLink('creationtime', 'CREATION');?></th>
			<th class="time sort"><?php echo __sortLink('date', 'MODIFIED');?></th>
			<th class="narrow center"><?php echo '<input type="checkbox" name="select-all" id="select-all" value="1" rel="id[]" />';?></th>
		</tr>
		<?php
		foreach($this->list AS $key => $row) {
			$categories = isset($this->categories[$row->getId()]) ? $this->categories[$row->getId()] : array();
			$keywords = isset($this->keywords[$row->getId()]) ? $this->keywords[$row->getId()] : array();
		?>
		<tr>
			<td class="icon"><a href="<?php echo $row->getCover()->getLarge(false);?>" target="_blank"><img src="<?php echo $row->getCover()->getThumb(false);?>" /></a></td>
			<td class="key center">
				<?php
				if(!$row->getIsActive()) {
					echo '<em class="red">Invalid</em>';
				} else {
					echo $this->form()->text('display_order[' . $row->getId() . ']', $row->getDisplayOrder(), array('size' => '5', 'maxlength' => '8'));
					echo $this->form()->hidden('_display_order[' . $row->getId() . ']', $row->getDisplayOrder());
				}
				?>
				<strong style="display:block;padding:4px 0;"><?php echo $row->getName();?></strong>
			</td>
			<td<?php if(!$row->getIsActive()) {echo ' class="gray"';}?>>
				<h3 class="title"><?php
				if($name == 'recommend') {
					if(1 && $_group = (isset($this->regroups[$row->getGroupId()]) ? $this->regroups[$row->getGroupId()] : false)) {
						echo '<span style="float:right;padding:2px 3px;border:1px dashed #333;">' . $_group->getDescription() . '</a></span>';
					}
				}
				?><a href="<?php echo $this->url('special/index', array('name' => $row->getName()))->assemble($row->getLang());?>" target="_blank"><?php echo $this->escape($row->getTitle());?></a></h3>
				<p class="tags">
					<span>[<?php echo $this->language('TAG');?>]: </span><?php
					$cs = $_cs = array(); 
					foreach($keywords AS $kw) {
						$cs[] = '<a href="' . $this->url('resource/keyword/edit', array('id' => $kw['keyword_id'])) . '">' . $this->escape($kw['name']) . '</a>';
						$_cs[] = $kw['name'];
					}
					echo '<span id="tag-' . $row->getId() . '">';
					echo implode($cs, ', ');
					if($row->getIsActive()) {
						echo '<a class="edit edit-modify" href="#tag" rel="' . $row->getId() . '">' . $this->language(empty($_cs) ? 'Add' : 'Change') . '»</a>';
					}
					echo '</span>';
					if($row->getIsActive()) {
						echo '<span id="tag-' . $row->getId() . '-input" class="hidden">';
						echo $this->form()->text('keyword[' . $row->getId() . ']', implode($_cs, ';'), array('rel' => implode($_cs, ';'), 'size' => '30', 'maxlength' => '150', 'style' => 'margin:0;padding:0 2px 1px 2px;', 'id' => 'keyword-' . $row->getId()));
						echo $this->form()->hidden('_keyword[' . $row->getId() . ']', implode($_cs, ';'));
						echo '<a class="edit edit-cancel" href="#tag" rel="' . $row->getId() . '">' . $this->language('Cancel') . '»</a>';
						echo '</span>';
					}
					?>
				</p>
				<p class="tags">
					<span>[<?php echo $this->language('CATEGORY');?>]: </span><span><?php
					$cs = array(); 
					foreach($categories AS $category) {
						$cs[] = '<a href="' . $this->url('picture/category/edit', array('id' => $category['category_id'])) . '">' . $this->escape($category['title']) . '</a>';
					}
					echo implode($cs, ', ');
					?></span>
				</p>
				<p class="toolbar">
					<a href="<?php echo $this->url('picture/special/edit', array('id' => $row->getId()));?>"><?php echo $this->language('Edit');?>»</a>
					<a href="<?php echo $this->url('picture/special/photo', array('id' => $row->getId()));?>"><?php echo $this->language('Photos');?>(<?php echo $row->getPtotal();?>)»</a>
					<a href="<?php echo $this->url('picture/photo/batch', array('special_id' => $row->getId()));?>" class="batch"><?php echo $this->language('Upload');?>»</a>
					<a href="<?php echo $this->url('picture/special/delete', array('id' => $row->getId()));?>"><?php echo $this->language('Delete');?>»</a>
				</p>
			</td>
			<td class="center<?php if(!$row->getPtotal()) echo ' gray';?>"><h1><a href="<?php echo $this->url('picture/special/photo', array('id' => $row->getId()));?>"><?php echo $row->getPtotal();?></a></h1></td>
			<td class="time"><?php echo $this->date($row->getCreationtime());?></td>
			<td class="time"><?php echo $this->date($row->getLastmodified());?></td>
			<td class="center"><?php
			if($row->getIsActive()) {
				$_id = $row->getId();
				if($name == 'recommend') {
					$_id = $row->getId() . " AND recommend.group_id = '{$row->getGroupId()}'";
				}
				if(in_array($_id, $this->checkedList)) {
					$checked = array('checked' => 'checked');
				} else {
					$checked = array();
				}
				echo $this->form()->checkbox('id[]', $_id, array_merge(array('id' => 'id-' . $row->getId()), $checked));
			}
			?></td>
		</tr>
		<?php } ?>
		<?php if($name == 'list' && count($this->list) <= 0) {?>
		<tr>
			<td colspan="7" class="nohover">
				<div class="msg">
					<div class="common error" style="border:0;background:#fff;">
						<i class="icon"></i>
						<h4>STOP</h4>
						<div class="msg-bd">
							No Special found. <br/>
							<a href="<?php echo $this->url('picture/special/addnew', array(), array('category_id' => intval($this->category_id)));?>"><?php echo $this->language('Create');?>..</a>
							Or 
							<a href="<?php echo $this->url('picture/import/special');?>"><?php echo $this->language('Import Special from other language')?> »</a>
						</div>
					</div>
				</div>
			</td>
		</tr>
		<?php } ?>
	</table>
</div>
<div class="body">
	<div style="float:right;margin-bottom:5px;">
		<button type="submit" name="saveChange" value="submit" onclick="return confirm('<?php echo $this->language('Are you sure to Save all changes');?>?');"><?php echo $this->language('Save Changes');?></button>
		<?php 
		if($name == 'recommend') {
			echo $this->partial('include/deleteButton.phtml', array('buttonName' => 'Remove'));
		} else {
			echo '<select name="group_id" style="padding:2px;margin:0 2px 0 0;">';
			foreach($this->regroups AS $group) {
				echo '<option value="' . $group->getId() . '">' . $this->escape($group->getDescription()) . '[' . $group->getName() . ']</option>';
			}
			echo '</select>';
			echo '<button type="submit" name="recommend" value="1">' . $this->language('Recommend') . '</button>';
			echo '<button type="submit" name="addTo" value="submit" style="margin-right:0;">' . $this->language('Add To') . '»</button>';
		}
		?>
	</div>
	<?php echo $this->paginator($this->list)->render();?>
</div>
</form>
